package cn.cdqf.web;

import cn.cdqf.bean.TStudent;
import cn.cdqf.common.ResultResponse;
import cn.cdqf.service.StudentService;
import cn.cdqf.utils.ExcelUtils;
import com.alibaba.fastjson.JSON;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
public class StudentServlet {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/queryStudent")
    @ResponseBody
    public String queryStudent(String name,long classId){
        List<TStudent> students = studentService.queryStudent(name,classId);
        return JSON.toJSONString(students);
    }

    @RequestMapping("/deleteStudent")
    @ResponseBody
    public ResultResponse deleteDepart(String id){
        int i = studentService.deleteStudent(Integer.parseInt(id));
        ResultResponse resultResponse =null;
        if (i>0){
            resultResponse = new ResultResponse(100,"删除成功");
        }else {
            resultResponse = new ResultResponse(200,"删除失败");
        }
        return resultResponse;
    }

    @RequestMapping("/queryStudentById")
    @ResponseBody
    public String queryStudentById(int id){
        TStudent student = studentService.queryStudentById(id);
        return JSON.toJSONString(student);
    }

    @RequestMapping("/updateStudent/{id}")
    @ResponseBody
    public ResultResponse updateStudent(TStudent tStudent, @PathVariable("id") int id){
        int i = studentService.updateStudent(id,tStudent);
        ResultResponse resultResponse = null;
        if (i> 0){
            resultResponse = new ResultResponse(100,"更新成功");
        }else {
            resultResponse = new ResultResponse(200,"更新失败");
        }
        return resultResponse;
    }

    @RequestMapping("/studentexcel")
    public void studentexcel(String name, long classId, HttpServletResponse response){
//        response.setContentType("utf-8");
        List<TStudent> students = studentService.queryStudent(name,classId);

        String [][] content = new String[students.size()][8];
        for (int i = 0; i <students.size() ; i++) {
            TStudent s = students.get(i);
            content[i][0]=String.valueOf(s.getId());
            content[i][1]=String.valueOf(s.getNo());
            content[i][2]=String.valueOf(s.getName());
            content[i][3]=String.valueOf(s.getSex());
            content[i][4]=String.valueOf(s.getEmail());
            content[i][5]=String.valueOf(s.getPhone());
            content[i][6]=String.valueOf(s.getCname());
            content[i][7]=String.valueOf(s.getSchool());
        }

        String[] title={"序号","学号","姓名","性别","邮箱","电话","班级","学校"};

        HSSFWorkbook wb = ExcelUtils.getHSSFWorkbook("学生信息统计", title, content);

        try {
            //excel文件名
            String fileName = "学生信息表" + System.currentTimeMillis() + ".xls";
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);//响应给客户端
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


}
